package com.baosight.sczx.zm.cs.service;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiBlock;
import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.core.threadlocal.UserSession;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.common.zm.cInterface.cs.Coalseam;
import com.baosight.sczx.common.zm.cInterface.cs.InitialData;
import com.baosight.sczx.common.zm.cInterface.cs.Miningarea;
import com.baosight.sczx.common.zm.cInterface.cs.RoadWay;
import com.baosight.sczx.zm.cs.domain.ZMCSWorkingFace;


public class ServiceZMCS04 extends ServiceEPBase {
	
	private String fdParentId = "";

	public EiInfo initLoad(EiInfo inInfo) {
		
		inInfo.addBlock(EiConstant.resultBlock).set("showCount", "true");// 如果不加这个，翻页的统计和显示是错误的
		inInfo.addBlock(EiConstant.resultBlock).set(EiConstant.limitStr, "100");//设置每页默认显示记录数
		//		EiInfo outInfo = this.query(inInfo);
	//	String fdMiningId = (String) UserSession.getInSessionProperty("miningCode");//矿ID的默认值		
		EiInfo outInfo = this.query(inInfo);
		return outInfo;
//		return inInfo;
	}
	
	public EiInfo query(EiInfo inInfo) {

		String fdMiningId = (String) UserSession.getInSessionProperty("miningCode");//矿ID的默认值
		fdParentId = inInfo.getString("fdParentId");
		String[] _nodeId=fdParentId.split("_");
		fdParentId=_nodeId[0];
		StaticTools.setQueryParameters(inInfo, "fdParentId", fdParentId, true);

		EiInfo outInfo = super.query(inInfo, "ZMCSWorkingFace.query", new ZMCSWorkingFace());
		outInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.cs.coalType", true));// 采煤方法
		outInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.cs.recoveryProcess", true));// 回采工艺
		outInfo.addBlock(RoadWay.getYesORNoBlock("cmes.cb.isFirst"));// 是否首采区
		outInfo.addBlock(Coalseam.getWorkStatusBlock("cmes.cb.status"));// 施工状态
		outInfo.addBlock(Miningarea.getParnetInforWfBlock());// 采面所属下拉框
		outInfo.addBlock(Coalseam.getParnetInforCBlock("cmes.cb.parentInforC", fdMiningId, true));// 所属煤层下拉框
		
		
//		inInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.cs.coalType", true));// 采煤方法
//		inInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.cs.recoveryProcess", true));// 回采工艺
//		inInfo.addBlock(RoadWay.getYesORNoBlock("cmes.cb.isFirst"));// 是否首采区
//		inInfo.addBlock(Coalseam.getWorkStatusBlock("cmes.cb.status"));// 施工状态
//		inInfo.addBlock(Miningarea.getParnetInforWfBlock());// 采面所属下拉框
//		inInfo.addBlock(Coalseam.getParnetInforCBlock("cmes.cb.parentInforC", fdMiningId, true));// 所属煤层下拉框
		
		outInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.cs.roadwayType", true));// 查询区'巷道类型'
		outInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.cs.mineRock", true));// 煤岩别
		outInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.cs.supportingType", true));// 支护形式
		outInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.cs.roadwayUsing", true));// 巷道用途
		outInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.cs.grubProcess", true));// 掘进工艺
//		inInfo.set("roadwayNoType", SystemSettingCS.getInstance().getRoadwayNo());// 巷道编号形式 0自动 1手动 2生成
		outInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.cs.sectionShape", true));// 断面形状
		outInfo.addBlock(RoadWay.getParnetInfoRoadWayBlock());// 巷道所属下拉框
		outInfo.addBlock(Coalseam.getWorkStatusBlock("cmes.cb.status"));// 施工状态

		
		return outInfo;
	}
	/**
	 * 
	 * <p>Function: update</p>
	 * <p>Description: 修改采面记录</p>
	 * <p>return value: EiInfo</p>
	 * <p>History:
	 *    
	 * </p>
	 *
	 */
	public EiInfo update(EiInfo inInfo) {
		EiInfo out=new EiInfo(); 
		ZMCSWorkingFace _dom = new ZMCSWorkingFace();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		int i = 0;
		for (; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			if (_dom.getFdIsFirst().equals("是"))// 保证只有一个采区首面
			{
				CheckFirst(_dom.getFdParentId());
			}
			this.dao.update("ZMCSWorkingFace.update",_dom);
		}
		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return out;
 	}
	
	/**
	 * 
	 * <p>Function: insert</p>
	 * <p>Description: 新增采面记录</p>
	 * <p>return value: EiInfo</p>
	 * <p>History:
	 *    
	 * </p>
	 *
	 */
	public EiInfo insert(EiInfo inInfo) {
//		fdParentId = inInfo.getString("fdParentId");
//		String[] _nodeId=fdParentId.split("_");
//		fdParentId=_nodeId[0];
		EiInfo out=new EiInfo(); 
		ZMCSWorkingFace _dom = new ZMCSWorkingFace();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		int i = 0;
		for (; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			String keyId = TablePk.getInstance().getNextValue("T_CI_WORKINGFACE");
			String fdName = _dom.getFdName();
			_dom.setFdKeyId(keyId); // 主键
			_dom.setFdAbbreviation(fdName); // 别名
			_dom.setFdDeleteStatus("1");
			if (_dom.getFdIsFirst().equals("是"))// 保证只有一个采区首面
			{
				CheckFirst(_dom.getFdParentId());
			}
			Map map=_dom.toMap();
			Iterator<Map.Entry<String, String>> it = map.entrySet().iterator();
			while (it.hasNext()) {
			   Map.Entry<String, String> entry = it.next();
			   map.put(entry.getKey(),entry.getValue().trim());
			}
			this.dao.insert("ZMCSWorkingFace.insert",map);
		}
		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return out;
	}
	/**
	 * 
	 * <p>Function: delete</p>
	 * <p>Description: 删除采面记录</p>
	 * <p>return value: EiInfo</p>
	 * <p>History:
	 *    
	 * </p>
	 *
	 */
	public EiInfo delete(EiInfo inInfo) {

		EiInfo out = new EiInfo();
		ZMCSWorkingFace _dom = new ZMCSWorkingFace();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();

		for (int i = 0; i < rowcount; i++) {
			String fdKeyId = inInfo.getBlock("result").getCellStr(i, "fdKeyId").toString();
			boolean validate = this.canDelete(inInfo, fdKeyId);// 采面是否被引用
			boolean isBeginWork = this.isBeginWork(inInfo, fdKeyId);// 采面是否开工
			if (validate) {
				return inInfo;
			} else if (isBeginWork) {
				return inInfo;
			}
			_dom.fromMap(eiBlock.getRow(i));
			this.dao.delete("ZMCSWorkingFace.delete", _dom);
		}
		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return out;
	}
	
	/**
	 * 校验编号名称简称不能重复
	 * <p>Function: validate</p>
	 * <p>Description: </p>
	 * <p>return value:boolean</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    
	 * </p>
	 *
	 */
	@SuppressWarnings("rawtypes")
	public EiInfo validate(EiInfo inInfo) {
		HashMap<String, String> params = new HashMap<String, String>();
		ZMCSWorkingFace _dom = new ZMCSWorkingFace();
		String oper=(String)inInfo.get("efCurButtonEname");
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		int i = 0;
		for (; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			String fdName = _dom.getFdName();
			String fdNo = _dom.getFdNo();
			String fdAbbreviation = _dom.getFdAbbreviation();
			String fdKeyId=_dom.getFdKeyId();
			params.put("fdName", fdName); // 工作面编号
			params.put("fdNo", fdNo); // 工作面名称
			params.put("fdAbbreviation", fdAbbreviation); // 工作面简称
			if(oper.equalsIgnoreCase("update"))
			{
				params.put("fdKeyId", fdKeyId);
			}
			List result = dao.query("ZMCSWorkingFace.validate", params);
			if (result != null && result.size() > 0) {
				inInfo.setStatus(-1);
				ZMCSWorkingFace _temp = (ZMCSWorkingFace)result.get(0);
				
				StringBuffer msg = new StringBuffer("提交失败，");
				if(_temp.getFdNo() != null && _temp.getFdNo().equals(fdNo)){
					msg.append(" 采面编号 ");
				}
				if(_temp.getFdName() != null && _temp.getFdName().equals(fdName)) {
					msg.append(" 采面名称 ");
				}
				msg.append("重复！");
				inInfo.setMsg(msg.toString());
				return inInfo;
			}
		}
		inInfo.setStatus(1);
		return inInfo;
	}
	
	/**
	 * 检查采面是否可以删除
	 * <p>Function: canDelete</p>
	 * <p>Description: </p>
	 * <p>return value:void</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2013-7-4下午01:32:59 高强
	 * </p>
	 *
	 */
	private boolean canDelete(EiInfo inInfo, String fdKeyId) {
		if (fdKeyId == null) {
			fdKeyId = new String("");
		}

		Map<String, String> _argMap = new HashMap<String, String>();
		_argMap.put("fdKeyId", fdKeyId);

		List<?> _result1 = dao.query("ZMCSRoadWay.canDelete", _argMap);

		if (_result1.size() > 0) {
			inInfo.setStatus(-1);
			inInfo.setMsg("包含有关联了巷道的采面，不能删除！");
			return true;

		}

		return false;
	}
	/**
	 * 检查采面是否开工
	 * <p>Function: isBeginWork</p>
	 * <p>Description: </p>
	 * <p>return value:void</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2013-7-11   
	 * </p>
	 *
	 */
	private boolean isBeginWork(EiInfo inInfo, String fdKeyId) {
		if (fdKeyId == null) {
			fdKeyId = new String("");
		}

		Map<String, String> _argMap = new HashMap<String, String>();
		_argMap.put("fdKeyId", fdKeyId);

		List<?> _result2 = dao.query("ZMCSWorkingFace.isBeginWork", _argMap);

		if (_result2.size() > 0) {
			inInfo.setStatus(-1);
			inInfo.setMsg("包含有施工状态为开工的采面，不能删除！");
			return true;

		}

		return false;
	}
	/**
	 * 保证首采区只有一条数据
	 * <p>Function: validate</p>
	 * <p>Description: </p>
	 * <p>return value: ServiceZMCS02</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *   2013-7-2上午11:03:09  高强
	 * </p>
	 *
	 */
	@SuppressWarnings("rawtypes")
	public void CheckFirst(String fdParentId) {
		Map _args = new HashMap();
		_args.put("fdParentId", fdParentId);
		// _args.put("fdKeyId", keyId);
		_args.put("fdIsFirst", "否");
		dao.update("ZMCSWorkingFace.updateFirst", _args);
	}
}
